package com.woniuxy.microsysserver.service.impl;
import com.woniuxy.bean.CertificationEntity;
import com.woniuxy.bean.MemberEntity;
import com.woniuxy.microsysserver.dao.BankCardDao;
import com.woniuxy.microsysserver.dao.MemberDao;
import com.woniuxy.microsysserver.entity.BankCardEntity;
import com.woniuxy.microsysserver.entity.CouponEntity;
import com.woniuxy.microsysserver.service.MemberService;
import com.woniuxy.microsysserver.uitl.Constant;
import com.woniuxy.microsysserver.uitl.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * Auther: wuson  <br/>
 * Date: 2021/3/11:19:51  <br/>
 * Description:
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired(required = false)
    private MemberDao memberDao;
    @Autowired(required = false)
    private BankCardDao bankCardDao;
    //根据账号查询
    @Override
    public MemberEntity selectByAccount(String username) {
        MemberEntity memberEntity = memberDao.selectByAccount(username);
        return memberEntity;
    }
    //注册
    @Override
    public int register(MemberEntity memberEntity) {
        String MD5psw = MD5.encrypt(memberEntity.getPassword());
        memberEntity.setPassword(MD5psw);
        int state = memberDao.register(memberEntity);
        return state;
    }
    //同过账号更新任意可更新字段
    @Override
    public int updateByName(MemberEntity memberEntity) {
        int state = memberDao.updateByName(memberEntity);
        return state;
    }
    //余额支付
    @Override
    public String balancePayment(String username, double money) {
        String status = Constant.SUCCESSFUL;
        MemberEntity currentMemberEntity = memberDao.selectByAccount(username);
        if(currentMemberEntity.getBalance()<money)
        {
                    status=Constant.INSUFFICIENT_BALANCE;
        }else {
            MemberEntity memberEntity = null;
            //赚钱
            if(money<0){
                memberEntity = MemberEntity.builder().username(currentMemberEntity.getUsername()).balance(currentMemberEntity.getBalance() - money).build();

            }else {//付钱
                 memberEntity = MemberEntity.builder().username(currentMemberEntity.getUsername()).balance(currentMemberEntity.getBalance() - money).integral(currentMemberEntity.getIntegral() + ((int) money) / 10).build();
            }
            memberDao.updateByName(memberEntity);
        }
        return status;
    }
    //实名认证
    @Override
    public int certification(CertificationEntity certification,String payPassword) {
        MemberEntity memberEntity = MemberEntity.builder().username(certification.getUsername()).payPassword(payPassword).idcardName(certification.getName()).idcard(certification.getIdcard()).sex(certification.getSex()).build();
        int state = memberDao.updateByName(memberEntity);
        return state;
    }
    //查询优惠券
    @Override
    public CouponEntity selectCouponById(int couponId) {
        CouponEntity couponEntity = memberDao.selectCouponById( couponId);
        System.out.println(couponEntity);
        return couponEntity;
    }
    //使用优惠券
    @Override
    public int userCoupon(int couponId) {
        int state = memberDao.userCoupon(couponId);
        return 0;
    }
    //更新密码
    @Override
    public int updatepsw(MemberEntity memberEntity) {
        MemberEntity updateMemberEntity = MemberEntity.builder().username(memberEntity.getUsername()).password(MD5.encrypt(memberEntity.getPassword())).build();
        int state = memberDao.updateByName(updateMemberEntity);
        return state;
    }

    @Override
    public String balancePaymentByBank(String memberName, Double money,int bankCardId) {
        String status = Constant.SUCCESSFUL;
        BankCardEntity bankCardEntity = bankCardDao.selectByid(bankCardId);
        if(bankCardEntity.getBalance()<money)
        {
            status=Constant.INSUFFICIENT_BALANCE;
        }else {
            bankCardDao.updateBalance(BankCardEntity.builder().id(bankCardId).memberUsername(memberName).balance(bankCardEntity.getBalance()-money).build());
        }
        return status;
    }

    @Override
    public int signIn(MemberEntity currentMemberEntity) {
        int signinNum = currentMemberEntity.getSignin();
        if(signinNum<7){
           signinNum+=1;
        }
        int state =  memberDao.signIn(MemberEntity.builder().username(currentMemberEntity.getUsername()).integral(currentMemberEntity.getIntegral()+signinNum*10).signin(signinNum).build());
        return state;
    }

    @Override
    public void signOut(String username) {
        memberDao.signOut(username);
    }



}
